package org.fall.system.config;

import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import org.fall.system.config.interceptor.AdminUserRoleHandlerInterceptor;
import org.fall.system.config.interceptor.LoginHandlerInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * @author zjh
 * @since 2021/4/7 16:05
 */
@Configuration
public class WebMvcConfig implements WebMvcConfigurer {
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        // 放行资源路径数组
        String[] permitUrls = {"/login.html", "/regist.html", "/css/**", "/fonts/**", "/images/**",
                "/js/**", "/lib/**", "/favicon.ico", "/admin/index/login", "/admin/index/register"};
        registry.addInterceptor(new LoginHandlerInterceptor()).excludePathPatterns(permitUrls);
        registry.addInterceptor(new AdminUserRoleHandlerInterceptor()).excludePathPatterns(permitUrls);
    }

    /**
     * MyBatisPlus分页插件配置
     * @return MybatisPlusInterceptor
     */
    @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor() {
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
        interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
        return interceptor;
    }
}
